package org.mark.user.service.impl;

import org.mark.common.response.Result;
import org.mark.user.entity.User;
import org.mark.user.mapper.UserMapper;
import org.mark.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Result login(User user) {
        if (StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getPassword())) {
            return Result.fail("用户名或密码不能为空");
        }

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName, user.getUserName())
                .eq(User::getPassword, user.getPassword());
        User existUser = userMapper.selectOne(wrapper);

        if (existUser == null) {
            return Result.fail("用户名或密码错误");
        }
        return Result.success(existUser);
    }

    @Override
    public Result register(User user) {
        // 验证必填字段
        if (StringUtils.isEmpty(user.getUserId())) {
            return Result.fail("用户ID不能为空");
        }
        if (StringUtils.isEmpty(user.getUserName())) {
            return Result.fail("用户名不能为空");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            return Result.fail("密码不能为空");
        }

        // 检查用户ID是否已存在
        LambdaQueryWrapper<User> userIdWrapper = new LambdaQueryWrapper<>();
        userIdWrapper.eq(User::getUserId, user.getUserId());
        if (userMapper.selectOne(userIdWrapper) != null) {
            return Result.fail("用户ID已存在");
        }

        // 检查用户名是否已存在
        LambdaQueryWrapper<User> userNameWrapper = new LambdaQueryWrapper<>();
        userNameWrapper.eq(User::getUserName, user.getUserName());
        if (userMapper.selectOne(userNameWrapper) != null) {
            return Result.fail("用户名已存在");
        }

        // 设置默认角色
        if (StringUtils.isEmpty(user.getRole())) {
            user.setRole("user");
        }

        userMapper.insert(user);
        return Result.success("");
    }

    @Override
    public Result getPage(Integer pageNum, Integer pageSize, String search) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(search)) {
            wrapper.like(User::getUserName, search);
        }

        Page<User> page = new Page<>(pageNum, pageSize);
        page = userMapper.selectPage(page, wrapper);
        return Result.success(page);
    }

    @Override
    public Result update(User user) {
        if (StringUtils.isEmpty(user.getUserId())) {
            return Result.fail("用户ID不能为空");
        }

        // 检查用户是否存在
        User existUser = userMapper.selectOne(
                new LambdaQueryWrapper<User>()
                        .eq(User::getUserId, user.getUserId())
        );

        if (existUser == null) {
            return Result.fail("用户不存在");
        }

        // 检查用户名是否已被其他用户使用
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName, user.getUserName())
                .ne(User::getUserId, user.getUserId());
        if (userMapper.selectOne(wrapper) != null) {
            return Result.fail("用户名已存在");
        }

        // 如果没有传入密码，使用原密码
        if (StringUtils.isEmpty(user.getPassword())) {
            user.setPassword(existUser.getPassword());
        }

        // 更新用户信息
        LambdaQueryWrapper<User> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(User::getUserId, user.getUserId());
        userMapper.update(user, updateWrapper);
        return Result.success("");
    }

    @Override
    public Result delete(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return Result.fail("用户ID不能为空");
        }

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserId, userId);
        int count = userMapper.delete(wrapper);

        if (count > 0) {
            return Result.success("");
        } else {
            return Result.fail("用户不存在");
        }
    }
}